package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.PartDAO;
import edu.byu.intex2.data.dao.PartServiceLineDAO;

/**
 * Tracks quantity used for every part on a service line
 * @author Brady White <satara@gmail.com>
 * @see ServiceLine
 */
public class PartServiceLine extends BusinessObject
{
    private int quantityUsed;

    private String serviceId;
    private String taskId;

    private String partId;
    private Part part;

    private ServiceLine serviceLine;

    /** psf */
    private PartsAtServiceFacility psf;

    /** Creates a new instance of BusinessObject */
    public PartServiceLine(String id)
    {
        super(id);
    }//constructor

    /** Convenience method to call the correct DAO to save this business object */
    public void save() throws DataException {
        try {
            PartServiceLineDAO.getInstance().save(this);
        } catch (DataException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return the quantityUsed
     */
    public int getQuantityUsed()
    {
        return quantityUsed;
    }

    /**
     * @param quantityUsed the quantityUsed to set
     */
    public void setQuantityUsed(int quantityUsed)
    {
        this.quantityUsed = quantityUsed;
        setDirty(true);
    }

    /**
     * @return the serviceId
     */
    public String getServiceId() {
        return serviceId;
    }

    /**
     * @param serviceId the serviceId to set
     */
    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
        setDirty(true);
    }

    /**
     * @return the taskId
     */
    public String getTaskId() {
        return taskId;
    }

    /**
     * @param taskId the taskId to set
     */
    public void setTaskId(String taskId) {
        this.taskId = taskId;
        setDirty(true);
    }

    /**
     * @return the partId
     */
    public String getPartId() {
        return partId;
    }

    /**
     * @param partId the partId to set
     */
    public void setPartId(String partId) {
        this.partId = partId;
        setDirty(true);
    }

    /**
     * @return the psf
     */
    public PartsAtServiceFacility getPsf() {
        return psf;
    }

    /**
     * @param psf the psf to set
     */
    public void setPsf(PartsAtServiceFacility psf) {
        this.psf = psf;
    }

    /**
     * @return the serviceLine
     */
    public ServiceLine getServiceLine() {
        return serviceLine;
    }

    /**
     * @param serviceLine the serviceLine to set
     */
    public void setServiceLine(ServiceLine serviceLine) {
        this.serviceLine = serviceLine;
    }

    /**
     * @return the part
     */
    public Part getPart() {
        if (part == null){
            try {
                part = PartDAO.getInstance().read(partId);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return part;
    }

    /**
     * @param part the part to set
     */
    public void setPart(Part part) {
        this.part = part;
        setPartId(part.getId());
    }

    @Override
    public String toString(){
        return quantityUsed + " - " + getPart().getName() + " - $" + getPart().getPrice();
    }
}
